﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.Windows.Forms;

namespace TeachMe.Application.Forms.Options
{
    public partial class Hotkeys : OptionsControl
    {
        public Hotkeys()
        {
            InitializeComponent();
        }

        internal override void LoadOptions()
        {
            Keys modifiers, key;

            if (Root.HotkeysManager.GetHotkey(HotkeyType.CreateEntry, out modifiers, out key))
            {
                heCreateEntryHotkey.Set(modifiers, key);
            }

            if (Root.HotkeysManager.GetHotkey(HotkeyType.DisplayEntry, out modifiers, out key))
            {
                heDisplayEntryHotkey.Set(modifiers, key);
            }

            if (Root.HotkeysManager.GetHotkey(HotkeyType.Suspend, out modifiers, out key))
            {
                heSuspendHotkey.Set(modifiers, key);
            }

            tbHotkeySuspendInterval.Value = UserSettings.HotkeySuspendInterval.Value;
        }

        internal override void SaveOptions()
        {
            Keys modifiers, key;

            if (heCreateEntryHotkey.Modified)
            {
                if (heCreateEntryHotkey.Get(out modifiers, out key))
                {
                    Root.HotkeysManager.SetHotkey(HotkeyType.CreateEntry, modifiers, key);
                }
                else
                {
                    Root.HotkeysManager.RemoveHotkey(HotkeyType.CreateEntry);
                }
            }

            if (heDisplayEntryHotkey.Modified)
            {
                if (heDisplayEntryHotkey.Get(out modifiers, out key))
                {
                    Root.HotkeysManager.SetHotkey(HotkeyType.DisplayEntry, modifiers, key);
                }
                else
                {
                    Root.HotkeysManager.RemoveHotkey(HotkeyType.DisplayEntry);
                }
            }

            if (heSuspendHotkey.Modified)
            {
                if (heSuspendHotkey.Get(out modifiers, out key))
                {
                    Root.HotkeysManager.SetHotkey(HotkeyType.Suspend, modifiers, key);
                }
                else
                {
                    Root.HotkeysManager.RemoveHotkey(HotkeyType.Suspend);
                }
            }

            UserSettings.HotkeySuspendInterval.Value = tbHotkeySuspendInterval.Value;
        }
    }
}
